#ifndef TCP_ACCEPTER_H
#define TCP_ACCEPTER_H

#include <functional>
#include <utility>
#include <netinet/in.h>

#include "Socket.h"
#include "Channel.h"

class EventLoop;

class InetAddress;

class Acceptor {
public:
    typedef std::function<void(int sockfd, const InetAddress &)> NewConnectionCallback;

    explicit Acceptor(EventLoop *loop, const InetAddress &listenAddr, bool reuseport);

    ~Acceptor();

    void listen();

    bool listening() const { return listening_; }

    void setNewConnectionCallback(NewConnectionCallback newConnectionCallback) {
        newConnectionCallback_ = std::move(newConnectionCallback);
    }

private:
    void handleRead();

    EventLoop *loop_;
    Socket acceptSocket_;
    // 保存对应的socket
    Channel acceptChannel_;
    NewConnectionCallback newConnectionCallback_;
    // 有新连接之后调用的回调函数
    bool listening_;
    // 是否在监听中
    int idleFd_;
    // 预先申请的空文件描述符, 用来处理请求溢出的情况
};

#endif //TCP_ACCEPTER_H
